<?php

namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%zs_task_item}}".
 *
 * @property integer $task_id
 * @property string $auth_item
 * @property integer $create_time
 */
class TaskItem extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%zs_task_item}}';
    }


    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['create_time'],
                ],
            ],


        ];
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['task_id', 'create_time'], 'integer'],
            [['auth_item'], 'string', 'max' => 300]
        ];
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'task_id' => '任务id',
            'auth_item' => '授权项',
            'create_time' => '创建时间',
        ];
    }

    /**
     * 根据任务id获取其下的授权项
     * @return array|bool
     */
    public function getItemsByTaskId()
    {
        if(!$this->task_id){
            return [];
        }

        $result = self::findAll([ 'task_id' => $this->task_id]);
        $routes = [];
        foreach($result as $item){
            $routes[] = $item->auth_item;
        }
        return $routes;
    }

    /**
     * 为了适应前端的效果，键和值都是一样的
     * @return array
     * @author zhangchao
     */
    public function getItemByTaskIdForKey()
    {
        $routes = $this->getItemsByTaskId();
        $result = [];
        foreach($routes as $route){
            $result[$route] = $route;
        }
        return $result;
    }
}
